package com.esm.sm800.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.dbmodel.Warehousing;
import com.esm.sm800.database.dbmodel.Warehousing_record;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.SystemCenter;
import com.esm.sm800.database.manager.WarehousingCenter;
import com.esm.sm800.database.manager.WarehousingrecordCenter;

import java.text.SimpleDateFormat;
import java.util.Date;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;

//新增编辑仓储
public class CcbjActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {

    private final String TAG = "CcbjActivity";

    private EditText et_model;
    private EditText et_brand;
    TextView tv_position;

    EditText et_stock;

    int _laststock = 0;//修改前的库存

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_ccbj);

        et_model = (EditText) findViewById(R.id.et_model);
        et_brand = (EditText) findViewById(R.id.et_brand);
        et_stock = (EditText) findViewById(R.id.et_stock);
        tv_position = (TextView) findViewById(R.id.tv_position);

        findViewById(R.id.tv_del).setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);
        findViewById(R.id.tv_position).setOnClickListener(this);

        if (SystemCenter.getInstance().warehousingEdit != null) {
            _isadd = false;
            et_model.setText(SystemCenter.getInstance().warehousingEdit.getModel());
            et_brand.setText(SystemCenter.getInstance().warehousingEdit.getBrand());
            _laststock = SystemCenter.getInstance().warehousingEdit.getStock();
            et_stock.setText(_laststock + "");
            _posType = SystemCenter.getInstance().warehousingEdit.getPosition();
        } else {
            findViewById(R.id.tv_del).setVisibility(View.GONE);
        }

        String[] TYPES = getResources().getStringArray(R.array.cc_types);
        if (_posType >= 0 && _posType < TYPES.length) {
            tv_position.setText(TYPES[_posType]);
        }
    }

    boolean _isadd = true;//false-编辑

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {

            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_save:
                onSave();
                break;
            case R.id.tv_del:
                onDel();
                break;
            case R.id.tv_position:
                onPosition();
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }

    private void onDel() {
        new AlertDialog(context, "", getString(R.string.qqdsfsc), true, 11, this).show();
    }

    private void onPosition() {
        toActivity(BottomMenuWindow.createIntent(getBaseContext(), getResources().getStringArray(R.array.cc_types))
                .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.cc_title)), REQUEST_TO_BOTTOM_MENU_1, false);
    }

    private void onSave() {
        //获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date();
        String strDate = formatter.format(curDate);
        //

        String model = et_model.getText().toString().replace(" ", "").replace("\n", "");
        String brand = et_brand.getText().toString().replace(" ", "").replace("\n", "");
        String stock = et_stock.getText().toString().replace(" ", "").replace("\n", "");

        if (model.length() < 1 || brand.length() < 1) {
            showShortToast(getString(R.string.qsrpphxh));
            return;
        }

        int num = 0;
        try {
            num = Integer.parseInt(stock);
        } catch (Exception e) {
            Toast.makeText(CcbjActivity.this, getString(R.string.cwdsz), Toast.LENGTH_SHORT).show();
            return;
        }

        Warehousing warehousing = SystemCenter.getInstance().warehousingEdit;
        if (warehousing == null) {
            warehousing = new Warehousing();
            warehousing.setCreat_time(strDate);
            warehousing.setUpdate_time(strDate);
        }
        warehousing.setBrand(brand);
        warehousing.setModel(model);
        warehousing.setStock(num);
        warehousing.setPosition(_posType);

        int save_num = 0; //存入数
        int get_num = 0; //取出数

        if (num > _laststock) {
            save_num = num - _laststock;
            warehousing.setUpdate_time(strDate);
        } else if (num < _laststock) {
            get_num = _laststock - num;
            warehousing.setUpdate_time(strDate);
        }
        Long warehousing_id = WarehousingCenter.getInstance().saveWarehousing(warehousing);

        if (num != _laststock) {
            //需记录
            String[] TYPES = getResources().getStringArray(R.array.member_types);

            Warehousing_record warehousing_record = new Warehousing_record();
            warehousing_record.setCreat_time(strDate);
            warehousing_record.setUpdate_time(strDate);
            warehousing_record.setBrand(brand);
            warehousing_record.setModel(model);
            warehousing_record.setStock(num);
            warehousing_record.setGet_num(get_num);
            warehousing_record.setSave_num(save_num);
            warehousing_record.setWarehousing_id(warehousing_id);
            String strPos = TYPES[_posType];
            warehousing_record.setPosition(strPos);
            if (SystemCenter.getInstance().member != null) {
                warehousing_record.setMember(SystemCenter.getInstance().member.getName());
            }
            WarehousingrecordCenter.getInstance().saveWarehousing_record(warehousing_record);
        }

        setResult(RESULT_OK);
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        et_model.clearFocus();
        et_brand.clearFocus();
        et_stock.clearFocus();

        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int pos = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    String[] TYPES = getResources().getStringArray(R.array.cc_types);
                    if (pos >= 0 && pos < TYPES.length) {
                        String name = TYPES[pos];
                        _posType = pos;
                        tv_position.setText(name);
                    }
                }
                break;
        }
    }

    int _posType = 0;

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }
        if (requestCode == 11) {
            //确定删除
            WarehousingCenter.getInstance().delWarehousing(SystemCenter.getInstance().warehousingEdit);
            setResult(RESULT_OK);
            finish();

        }
    }
}
